package com.codergd.gra.controller;

import cn.hutool.core.io.FileUtil;
import com.codergd.gra.bean.User;
import com.codergd.gra.service.UserService;
import com.codergd.gra.utils.FileTran;
import com.codergd.gra.utils.QCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private FileTran fileTran;
//    @Autowired
//    private QCloudUtil qCloudUtil;

    @Value("${files.upload.path}")
    private String path;

    @PostMapping("/isLogin")
    public Object isLogin(String iphone,@RequestParam("password") String pwa){
        return userService.isLogin(iphone, pwa);
    }

    @PostMapping("/registered")
    public int registered(User user) {
        return userService.registered(user);
    }

    //根据手机号查找id
    @PostMapping("/selectId")
    public int selectId(HttpServletRequest request){
        return userService.selectId(request.getParameter("iphone"));
    }

    @GetMapping("/hi")
    public String hi() {
        return "hello";
    }


    //修改简历
    @PostMapping("/upLoadResume")
    public int changeResume(@RequestParam("file") MultipartFile file,
                            @RequestParam("id") int id){
        //保存路径
        //springboot 默认情况下只能加载 resource文件夹下静态资源文件
        String dFileName = fileTran.FileTran(file,path);
        if(dFileName == null) {
            return 0;
        }
        // 删除本地文件
//        File img = new File(path+dFileName);
//        if (img.delete()){
//            System.out.println("文件已删除：" + dFileName);
//        }else {
//            System.out.println("删除失败");
//        }
        //修改数据库中文件名
        return userService.changeResume(id,dFileName);
    }

    @GetMapping("/{fileUUID}")
    public void getResume(@PathVariable String fileUUID, HttpServletResponse response) throws IOException {
        File uploadFile = new File(path + fileUUID);
        ServletOutputStream os = response.getOutputStream();
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileUUID, "UTF-8"));
        response.setContentType("application/octet-stream");
        os.write(FileUtil.readBytes(uploadFile));
        os.flush();
        os.close();
    }

    //更换头像
    @PostMapping("/upLoadImg--")
    public int changeHead(@RequestParam("key") String key,
                          @RequestParam("id") int id){
        System.out.println("收到的id和key是："+id+key);

        //修改数据库中文件名
        return userService.changeHead(id,key);


        // //保存路径
        // //springboot 默认情况下只能加载 resource文件夹下静态资源文件
        // String path = "D:\\360downloads\\tempimg\\";
        // String dFileName = fileTran.FileTran(file,path);
        //
        //
        // // 删除本地文件
        // File img = new File(path+dFileName);
        // if (img.delete()){
        //     System.out.println("文件已删除：" + dFileName);
        // }else {
        //     System.out.println("删除失败");
        // }
    }

    @PostMapping("/upLoadImg")
    public int uploadImage(@RequestParam("file") MultipartFile file,
                            @RequestParam("id") int id) {
        String dFileName = fileTran.FileTran(file,path);
        return userService.changeHead(id,dFileName);
    }

    //修改账号信息
    @PostMapping("/changeInfo")
    public int changeInfo(int id,String name,String iphone,String paw){
        return userService.changeInfo(id, name, iphone, paw);
    }

    //修改意向
    @PostMapping("/changeWant")
    public int changeWant(int id,String work,String city,int sal,String content,String work1,int sal1){
        return userService.changeWant(id, work, city, sal, content,work1,sal1);
    }

    //根据id查看个人信息
    @PostMapping("/getUserInfo")
    public User getUserInfo(int id){
        return userService.getUserInfo(id);
    }

}
